#!/bin/bash
baseDirForScriptSelf=$(cd "$(dirname "$0")"; pwd)
if [ -f /etc/apt/sources.list ];then
    sudo mv /etc/apt/sources.list /etc/apt/sources.list_`date '+%s'`
fi
unset http_proxy
unset https_proxy
is_huawei_tmp=`curl -I -m 10 -o /dev/null -s -w %{http_code} w3.huawei.com`
if [ "${is_huawei_tmp}" == "200" ];then
    is_huawei=true
else
    is_huawei=false
fi
# lsb_release 方式获取版本号
# 判断系统类型
os_type=`lsb_release  -i|awk '{print $NF}'|tr '[A-Z]' '[a-z]'`
# 判断OS版本号
os_version=`lsb_release -r|awk '{print $NF}'`
if [ "${os_type}${os_version}" == "ubuntu18.04" -a  "${is_huawei}" == "true" ];then
    source_domain=mirrors.tools.huawei.com
    source_os_version=bionic
elif [ "${os_type}${os_version}" == "ubuntu18.04" -a  "${is_huawei}" == "false" ];then
    source_domain=mirrors.aliyun.com
    source_os_version=bionic
elif [ "${os_type}${os_version}" == "ubuntu20.04" -a  "${is_huawei}" == "true" ];then
    source_domain=mirrors.tools.huawei.com
    source_os_version=focal
elif [ "${os_type}${os_version}" == "ubuntu20.04" -a  "${is_huawei}" == "false" ];then
    source_domain=mirrors.aliyun.com
    source_os_version=focal
elif [ "${os_type}${os_version}" == "debian9.0" -a  "${is_huawei}" == "true" ];then
    source_domain=mirrors.tools.huawei.com
    source_os_version=stretch
elif [ "${os_type}${os_version}" == "debian9.0" -a  "${is_huawei}" == "false" ];then
    source_domain=mirrors.aliyun.com
    source_os_version=stretch
else
    echo '不支持的操作系统,请检查'
fi
if [ "${os_type}" == "ubuntu" ];then
    sudo bash -c "echo '''deb http://${source_domain}/${os_type}/ ${source_os_version} main restricted universe multiverse
deb http://${source_domain}/${os_type}/ ${source_os_version}-security main restricted universe multiverse
deb http://${source_domain}/${os_type}/ ${source_os_version}-updates main restricted universe multiverse
deb http://${source_domain}/${os_type}/ ${source_os_version}-proposed main restricted universe multiverse
deb http://${source_domain}/${os_type}/ ${source_os_version}-backports main restricted universe multiverse
deb-src http://${source_domain}/${os_type}/ ${source_os_version} main restricted universe multiverse
deb-src http://${source_domain}/${os_type}/ ${source_os_version}-security main restricted universe multiverse
deb-src http://${source_domain}/${os_type}/ ${source_os_version}-updates main restricted universe multiverse
deb-src http://${source_domain}/${os_type}/ ${source_os_version}-proposed main restricted universe multiverse
deb-src http://${source_domain}/${os_type}/ ${source_os_version}-backports main restricted universe multiverse''' >/etc/apt/sources.list"
elif [ "${os_type}" == "debian" ];then
    sudo bash -c "echo '''deb http://${source_domain}/${os_type} ${source_os_version} main non-free contrib
deb-src http://${source_domain}/${os_type} ${source_os_version} main non-free contrib
deb http://${source_domain}/${os_type} ${source_os_version}-updates main non-free contrib
deb-src http://${source_domain}/${os_type} ${source_os_version}-updates main non-free contrib
deb http://${source_domain}/${os_type} ${source_os_version}-proposed-updates main non-free contrib
deb-src http://${source_domain}/${os_type} ${source_os_version}-proposed-updates main non-free contrib
deb http://${source_domain}/${os_type} ${source_os_version}-backports main non-free contrib
deb-src http://${source_domain}/${os_type} ${source_os_version}-backports main non-free contrib
deb http://${source_domain}/${os_type}-security ${source_os_version}/updates main non-free contrib
deb-src http://${source_domain}/${os_type}-security ${source_os_version}/updates main non-free contrib''' >/etc/apt/sources.list"
else
    echo '不支持的操作系统,请检查'
fi
sudo apt-get update
